package com.htd.apm.Controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.htd.htdapmdmo.OneAlertKey;
import com.htd.htdapmservice.service.intf.OneAlertKeyService;

@RestController
public class OneAlertKeyController {

    @Autowired
    private OneAlertKeyService oneAlertKeyService;

    @PostMapping("addOneAlertKey")
    public ResponseEntity addOneAlertKey(
            @RequestParam String productCode, @RequestParam String productKeyt){
        OneAlertKey oneAlertKey = new OneAlertKey();
        if(StringUtils.isBlank(productCode)){
            return new ResponseEntity("ProductCode不能为空", HttpStatus.INTERNAL_SERVER_ERROR);
        }
        if(StringUtils.isBlank(productKeyt)){
            return new ResponseEntity("ProductKeyt不能为空", HttpStatus.INTERNAL_SERVER_ERROR);
        }
        oneAlertKey.setProductCode(productCode);
        oneAlertKey.setProductKeyt(productKeyt);
        oneAlertKey.setCreateTime(new Date());
        oneAlertKey.setUpdateTime(new Date());
        return new ResponseEntity(oneAlertKeyService.addOneAlertKey(oneAlertKey), HttpStatus.OK);
    }

    @DeleteMapping("delOneAlertKey")
    public ResponseEntity delOneAlertKeyByProductCode(@RequestParam String productCode){
        if(StringUtils.isBlank(productCode)){
            return new ResponseEntity("productCode不能为空", HttpStatus.INTERNAL_SERVER_ERROR);
        }
        int count  = oneAlertKeyService.delOneAlertKeyByProductCode(productCode);
        if (count>0){
            return new ResponseEntity(count, HttpStatus.OK);
        }else{
            return new ResponseEntity(count, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("updataOneAlertKey")
    public ResponseEntity updataOneAlertKey(
            @RequestParam String productCode, @RequestParam String productKeyt){
        OneAlertKey oneAlertKey = new OneAlertKey();
        if(StringUtils.isBlank(productCode)){
            return new ResponseEntity("productCode不能为空", HttpStatus.INTERNAL_SERVER_ERROR);
        }
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("productCode",productCode);
        map.put("productKeyt",productKeyt);
        map.put("updateTime",new Date());
        int count  = oneAlertKeyService.updataOneAlertKeyByProductCode(map);
        if (count>0){
            return new ResponseEntity(count, HttpStatus.OK);
        }else{
            return new ResponseEntity(count, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @PostMapping("findOneAlertKeyByProductCode")
    public ResponseEntity findOneAlertKeyByProductCode(@RequestParam String productCode){
        OneAlertKey oneAlertKey = oneAlertKeyService.findOneAlertKeyByProductCode(productCode);
        return new ResponseEntity(oneAlertKey, HttpStatus.OK);
    }

}
